package com.bjsxt.controller;

import com.bjsxt.pojo.Dept;
import com.bjsxt.service.DeptService;
import com.bjsxt.service.impl.DeptServiceImpl;
import com.bjsxt.util.PageBean;
import com.google.gson.Gson;
import org.apache.ibatis.annotations.Insert;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

@WebServlet("/sxt/DeptServlet")
public class DeptServlet extends BaseServlet {

     private DeptService  deptService=new DeptServiceImpl();


     //Ajax方式查询部门操作
     private void deptFindAll2(HttpServletRequest req, HttpServletResponse resp) throws IOException {

         List<Dept> list = deptService.findAll2();

         String json = new Gson().toJson(list);
         resp.getWriter().print(json);

     }


         //删除部门实现
    private void deptRemove(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        int deptno = Integer.parseInt(req.getParameter("deptno"));

        int remove = deptService.remove(deptno);

        resp.sendRedirect(req.getContextPath()+"/sxt/DeptServlet?method=deptFindAll");

    }

    //修改部门实现
    private void deptChange(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //【A】接受前台页面中数据
        Integer deptno =Integer.parseInt(req.getParameter("deptno"));
        String deptname = req.getParameter("deptname");
        String location = req.getParameter("location");
        Dept  dept=new Dept(deptno,deptname,location);
        //【B】数据处理
        int change = deptService.change(dept);

        //【C】做出响应
        if(change>0){
            resp.sendRedirect(req.getContextPath()+"/sxt/DeptServlet?method=deptFindAll");
        }else{

            req.setAttribute("msg","修改失败");
            req.getRequestDispatcher("/deptUpdate.jsp").forward(req,resp);
        }
    }

    //查询单个部门实现
    private void deptFindOne(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //【A】接受前台页面中数据
        int deptno = Integer.parseInt(req.getParameter("deptno"));
        //【B】数据处理
        Dept dept = deptService.findOne(deptno);
        //【C】做出响应
        req.setAttribute("dept",dept);
        req.getRequestDispatcher("/deptUpdate.jsp").forward(req,resp);

    }

    //查询所有部门实现
    private void deptFindAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //【A】接受前台页面中数据

        String index = req.getParameter("index");

        int ind=1;
        if(index!=null &&!"".equals(index)){
            ind=Integer.parseInt(index);
        }

        PageBean  pg=new PageBean();
        pg.setIndex(ind);
        //设置总条数
        pg.setTotalCount(deptService.getCount());

        //获得开始下标
        int start = pg.getStartRow();
        //获得每页显示条数
        int size = pg.getSize();


        //【B】数据处理，返回结果
        List<Dept> list = deptService.findAll(start,size);
        pg.setList(list);
        //【C】根据结果做出响应
        req.setAttribute("pg",pg);
        req.getRequestDispatcher("/deptList.jsp").forward(req,resp);



    }

    //添加部门操作
    private void deptSave(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {

         //【A】接受前台页面中数据
          Integer  deptno=Integer.parseInt(req.getParameter("deptno"));
          String deptname = req.getParameter("deptname");
          String location = req.getParameter("location");

          Dept  dept=new Dept(deptno,deptname,location);
        //【B】数据的处理，返回结果--连接数据库
        int save = deptService.save(dept);

        //【C】根据结果，给前台用户做出响应
        if(save>0){
            //重定向到查询页面   /sxtoa/
            resp.sendRedirect(req.getContextPath()+"/sxt/DeptServlet?method=deptFindAll");
        }else{
            req.setAttribute("msg","添加失败");
            req.getRequestDispatcher("/deptAdd.jsp").forward(req,resp);
        }


    }
}
